package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.FoodMenu;
import cn.b.training.baby_education_training.entity.FoodMenuCollection;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.FoodMenuCollectionRepository;
import cn.b.training.baby_education_training.repository.FoodMenuRepository;
import cn.b.training.baby_education_training.repository.UserInfoRepository;
import cn.b.training.baby_education_training.service.MenuCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 21:28
 **/
@Service
public class MenuCollectionServiceImpl implements MenuCollectionService {
    @Autowired
    private FoodMenuCollectionRepository foodMenuCollectionRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private FoodMenuRepository foodMenuRepository;
    @Override
    public boolean addMenuCollection(FoodMenuCollection foodMenuCollection) throws Exception {
        UserInfo userInfo = userInfoRepository.getByUserId(foodMenuCollection.getUserId());
        FoodMenu foodMenu = foodMenuRepository.getFoodMenuById(foodMenuCollection.getFoodMenuId());
        if (userInfo!=null&&foodMenu!=null){
            foodMenuCollection.setUserId(userInfo.getId());
            foodMenuCollection.setFoodMenuId(foodMenu.getId());
            foodMenuCollection = foodMenuCollectionRepository.save(foodMenuCollection);
        }else {
            throw new Exception("信息不存在");
        }
        if (foodMenuCollection!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<FoodMenuCollection> getMenuCollectionByUserId(Long userId) {
        List<FoodMenuCollection> collections = foodMenuCollectionRepository.getMenuCollectionByUserId(userId);
        return collections;
    }

    @Override
    public Boolean deleteMenuCollectionById(Long id) {
        try{
            this.foodMenuCollectionRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
